#include<bits/stdc++.h>
using namespace std;
struct node{
	int num;
	string name;
};
int cmp(node a,node b){
	if(a.num!=b.num){
		return a.num>b.num;
	}
	else{
		return a.name<b.name;
	}
}
int main(){
	int n,k;
	cin >> n >> k;
	vector<node> list(n);
	for(int i = 0;i < n;i++){
		cin >> list[i].name >> list[i].num;
	}
	sort(list.begin(),list.end(),cmp);
	for(int i = 0;i < n;i++){
		cout << list[i].num << ' ';
	} 
	cout << endl;
	int t = 0,row = k,m;
	while(row){
		if(row==k){
			m = n-n/k*(k-1);
		}
		else{
			m = n/k;
		}
		vector<string> s(m);
		s[m/2] = list[t].name;
		int j = m/2-1;
		for(int i = t+1;i<t+m;i+=2){
			s[j--] = list[i].name;
		}
		j = m/2+1;
		for(int i = t+2;i<t+m;i+=2){
			s[j++] = list[i].name;
		}
		cout << s[0];
		for(int i = 1;i < m;i++) cout << " " << s[i];
		cout << endl;
		t+=m;row--;
	}
	
	return 0;
}

